Skip to content

Comments

Add Hande/Raise convenience syntax#684

Open
satorg wants to merge 2 commits intotypelevel:mainfrom
satorg:add-raise-syntax
Open

Add Hande/Raise convenience syntax#684
satorg wants to merge 2 commits intotypelevel:mainfrom
satorg:add-raise-syntax

Conversation

@satorg
Copy link

@satorg satorg commented Feb 4, 2026

Handle.allow approach is great but it lacks some convenience syntax that is avaliable in Cats. This PR proposes the following syntax:

  • for Option:

    • liftTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.option.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Some(123).liftTo[Try]("OOPS")
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Success(123))
      
      val res2 =
        Handle.allow:
          None.liftTo[Try]("OOPS")
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Failure(MyErr("OOPS")))    
    • raiseTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.option.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Some("OOPS").raiseTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Failure(MyErr("OOPS")))
      
      val res2 =
        Handle.allow:
          (None: Option[String]).raiseTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Success(()))
  • for Either:

    • liftTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.either.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Right[String, Int](123).liftTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Success(123))
      
      val res2 =
        Handle.allow:
          Left[String, Int]("OOPS").liftTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Failure("OOPS"))

@satorg satorg force-pushed the add-raise-syntax branch 2 times, most recently from a3c9bec to 756eaad Compare February 21, 2026 05:08
@satorg
Copy link
Author

satorg commented Feb 23, 2026

@LukaJCB thank you for the approval!
Just FYI: I still cannot merge it, perhaps I don't have write access here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants